import java.util.Random;

public class Guess {
	final static int NUM_COLORS = 4;
	final static int GUESS_SIZE = 4;
	private int values[] = new int[NUM_COLORS];
	private boolean solution;

	// Constructor for Solution
	// Randomly Generates Peg Values 1 - NUM_COLORS
	public Guess() {
		Random generator = new Random();
		for (int i = 0; i < NUM_COLORS; i++) {
			values[i] = generator.nextInt(NUM_COLORS) + 1;
		}
		solution = true;
	}

	// Constructor for User Submitted Guess
	public Guess(int user_values[]) {
		if (user_values.length == GUESS_SIZE) {
			for (int i = 0; i < GUESS_SIZE; i++) {
				if (user_values[i] < 1 || user_values[i] > NUM_COLORS) {
					System.exit(1);
				}
			}
			values = user_values;
		}
	}

	// counts the number of pegs in the guess that are the right color in the right spot
	public static int countBlack(Guess user_guess, Guess solution) {
		int count = 0;
		for (int i = 0; i < NUM_COLORS; i++) {
			if (user_guess.values[i] == solution.values[i])
				count++;
		}
		return count;
	}

	// counts the number of pegs in teh guess that are the right color in the wrong spot
	public static int countWhite(Guess user_guess, Guess solution) {
		int count = 0;
		int[] marks = new int[GUESS_SIZE];
		int[] blacks = new int[GUESS_SIZE];

		for (int i = 0; i < NUM_COLORS; i++) {
			blacks[i] = 0;
			marks[i] = 0;
		}

		for (int i = 0; i < NUM_COLORS; i++) {
			if (user_guess.values[i] == solution.values[i])
				blacks[i] = 1;
		}

		for (int i = 0; i < NUM_COLORS; i++) {
			for (int j = 0; j < NUM_COLORS; j++) {
				if (blacks[i] == 1)
					break;
				if (user_guess.values[j] == solution.values[i]) {
					if ((marks[j] == 0) && (blacks[j] == 0)) {
						count++;
						marks[j] = 1;
						break;
					}
				}
			}
		}
		return count;
	}

	public String toString() {
		String result = "";
		for (int i = 0; i < GUESS_SIZE; i++)
			result = result + this.values[i];
		return result;
	}

	public int[] getGuessArray() {
		return this.values;
	}
}